package com.example.controller;

import com.example.common.Result;
import com.example.entity.User;
import com.example.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 功能：用户控制器
 * 日期：2024/1/12 15:18
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    // 新增用户
    @PostMapping("/add")
    public Result addUser(@RequestBody User user) {
        userService.addUser(user);
        return Result.success();
    }

    // 修改用户
    @PutMapping("/update")
    public Result updateUser(@RequestBody User user) {
        userService.updateUser(user);
        return Result.success();
    }

    // 根据id获取用户
    @GetMapping("/getUserByUid/{uid}")
    public Result getUserByUid(@PathVariable("uid") Integer uid) {
        User user = userService.getUserByUid(uid);
        return Result.success(user);
    }
    // 根据account获取用户
    @GetMapping("/getUserByAccount/{account}")
    public Result getUserByAccount(@PathVariable("account") String account) {
        User user = userService.getUserByAccount(account);
        return Result.success(user);
    }

    // 获取所有用户
    @GetMapping("/getAll")
    public Result getAll(User user) {
        List<User> users = userService.getAll(user);
        return Result.success(users);
    }

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(User user,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<User> userPageInfo = userService.selectPage(user, pageNum, pageSize);
        return Result.success(userPageInfo);
    }

    // 删除用户
    @DeleteMapping("/delete/{uid}")
    public Result deleteUserByUid(@PathVariable("uid") Integer uid) {
        userService.deleteUserByUid(uid);
        return Result.success();
    }

    // 批量删除用户
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        userService.deleteBatch(ids);
        return Result.success();
    }

}